package cn.lysj.framework.api.controller;

import cn.lysj.framework.api.base.ApiContent;

import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;

import cn.lysj.framework.common.utils.websocket.IncidentType;
import cn.lysj.framework.common.utils.websocket.WebSocketServerToStore;
import cn.lysj.framework.order.domain.FoodOrder;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;


/**
 * @ Author ：drj.
 * @ Date  ：Created in 9:14 2018/11/13
 * @ Description:
 **/
@RestController
@RequestMapping("/api/v1/user")
@Api(value = "ApiOrderController", tags = "订单模块接口")
public class ApiOrderController extends ApiContent {


    @PostMapping("/released_order")
    public Resp addFoodOrder(String customerId, String eatTableId, String orderDetails,String custEatTableId) {
        return foodOrderService.addFoodOrder(orderDetails, customerId, eatTableId, custEatTableId);
    }

    @GetMapping("/show_food_order")
    public Resp showFoodOrder(String customerId,String storeId, PageVo pageVo) {

        return foodOrderService.showFoodOrder(customerId,storeId, pageVo);

    }

    /**
     * @author drj
     * @date 2018/11/15 10:15
     * @Description:用户发起退款申请
     */
    @PostMapping("/apply_for_refund")
    public Resp applyForRefund(String foodOrderId) {
        if (ParamUtil.isBlank(foodOrderId)) {
            return new Resp(Resp.PARAM_ERROR, "订单项id为空");
        }
        FoodOrder foodOrder = foodOrderService.getRepository().findOne(foodOrderId);
        if (foodOrder == null) {
            return new Resp(Resp.PARAM_ERROR, "订单错误");
        }
        //退款状态为1已支付或者2已结单才支持退款
        if (foodOrder.getFoodOrderStatus() == 1 || foodOrder.getFoodOrderStatus() == 2) {
            foodOrder.setFoodOrderStatus(3);
            foodOrderService.getRepository().save(foodOrder);
            WebSocketServerToStore.sendDataToMorePeopleMap(IncidentType.SEND_REFUND_RADIO, foodOrder, foodOrder.getStoreId(), true);
            return new Resp("退款申请提交成功");
        } else {
            return new Resp(Resp.PARAM_ERROR,"该订单不允许退款");
        }
    }
}
